<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>7.搜索--watch</title>
        <!-- 引入Vue -->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!-- 准备好一个容器-->
        <div id="app">
            <input type="search" v-model="search" /><br/><br/><br/>
            <ul>
                <li v-for="item in student2" :key="item.id">
                    {{item.name}}----{{item.age}} --- {{item.gender}}
                </li>
            </ul>
        </div>
    </body>

    <script type="text/javascript">
        //阻止 vue 在启动时生成生产提示。
        Vue.config.productionTip = false
        let vm = new Vue({
            el:"#app",
            data:{
                student:[
                    {id:1,name:"白永升",age:18,gender:"男"},
                    {id:2,name:"韩冰冰",age:16,gender:"女"},
                    {id:3,name:"刘畅",age:18,gender:"男"},
                    {id:4,name:"徐洁",age:18,gender:"女"},
                    {id:5,name:"见欢龙",age:18,gender:"男"}
                ],
                student2:[],
                search:""
            },
            watch:{
                search:{
                    immediate:true,
                    handler(newValue){
                    // console.log(newValue)
                    let newstudent = this.student.filter(item =>{
                        //indexOf  查找字符串在某个字符串中的位置
                        return item.name.indexOf(newValue) !== -1;
                    })

                    this.student2 = newstudent;
                }
                }
            }
        })

        // console.log(vm)
    </script>
</html>